package cate.game.rank.h;

import cate.common.table.d.GDCross;
import cate.common.table.d.GDRank;
import cate.game.rank.po.Rank;
import cate.game.rank.po.RankSPO;

import java.util.List;

public class RankLevelHandler extends RankHandler<RankLevelHandler.RankPointRaceSPO, Rank> {
    @Override
    protected String getTag() {
        return "level";
    }

    @Override
    protected RankPointRaceSPO createSPO() {
        return new RankPointRaceSPO();
    }

    @Override
    public int getRankId() {
        return GDRank.Id.LEVEL_RANK_ID;
    }

    /** 计算世界等级 */
    public int calcWorldLevel() {
        List<Rank> list = getRankList(null);
        if (list != null) {
            int num = Math.min(list.size(), GDCross.WORLD_LEVEL_PART_TWO_SIZE);
            if(num == 0){
                return 0;
            }
            long partOneSum = 0;
            long partTwoSum = 0;
            for (int i = 0; i < num; i++) {
                Rank item = list.get(i);
                if (i < GDCross.WORLD_LEVEL_PART_ONE_SIZE) {
                    partOneSum += item.value;
                }
                partTwoSum += item.value;
            }
            if (partOneSum == 0) {
                return 0;
            }
            double partOneAvg = (double) partOneSum / Math.min(GDCross.WORLD_LEVEL_PART_ONE_SIZE, num) * GDCross.WORLD_LEVEL_PART_ONE_SCALE;
            double partTwoAvg = (double) partTwoSum / num * GDCross.WORLD_LEVEL_PART_TWO_SCALE;
            return (int) (partOneAvg + partTwoAvg);
        }
        return 0;
    }

    public static final class RankPointRaceSPO extends RankSPO<Rank>{
        @Override
        protected int getRankNum() {
            return 100;
        }
    }
}
